﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using PhysicsControlModule.Daos;
using PhysicsControlModule.Models;
using PhysicsControlModule.Security;

namespace PhysicsControlModule.Controllers
{
    public class LabController : Controller
    {
        //
        // GET: /Lab/

        [Authorize(Roles = "STUDENT")]
        public ActionResult AdmissionIntro(int labworkId)
        {
            User ownerUser = UserDao.GetById(labworkId);
            return View(
                new AdmIntro
                    {
                        LabworkId = labworkId,
                        LabworkTitle = LabworkDao.GetById(labworkId).Title,
                        OwnerName = string.Format("{0} {1}", ownerUser.FirstName, ownerUser.LastName),
                        AdmissionQuestionsCount = AdmQuestionDao.GetCount(labworkId),
                        ControlQuestionsCount = CtrlQuestionDao.GetCount(labworkId)
                    });
        }

        [Authorize(Roles = "STUDENT")]
        public ActionResult AdmissionQuestion(int labworkId)
        {
            AdmQuestion admissionQuestion = AdmQuestionDao.GetUnanswered(labworkId, MmShProvider.GetUser().Id);
            if (admissionQuestion == null)
                return RedirectToAction("AdmissionResult", new { labworkId });

            return View(
                new AdmQuestionModel
                    {
                        Id = admissionQuestion.Id,
                        Content = admissionQuestion.Content,
                        Answers = AdmAnswerDao.Load(labworkId, admissionQuestion.Id),
                        SelectedAnswer = -1
                    });
        }

        [Authorize(Roles = "STUDENT")]
        [HttpPost]
        public ActionResult AdmissionQuestion(int labworkId, AdmQuestionModel admQuestionModel)
        {
            using (CmDbEntities db = new CmDbEntities())
            {
                db.AddToRelLabworkUserAdmQuestionAnswer(
                    new RelLabworkUserAdmQuestionAnswer
                        {
                            UserId = MmShProvider.GetUser().Id,
                            AnswerId = admQuestionModel.SelectedAnswer,
                            LabworkId = labworkId,
                            QuestionId = admQuestionModel.Id,
                            DatePublic = DateTime.Now
                        });
                db.SaveChanges();
            }
            return RedirectToAction("AdmissionQuestion", new { labworkId });
        }

        [Authorize(Roles = "STUDENT")]
        public ActionResult AdmissionResult(int labworkId)
        {
            return View(
                new AdmResultModel
                    {
                        LabworkId = labworkId,
                        LabworkTitle = LabworkDao.GetById(labworkId).Title,
                        TotalCount = AdmQuestionDao.GetCount(labworkId),
                        RightCount = AdmQuestionDao.GetRightCount(labworkId, MmShProvider.GetUser().Id)
                    });
        }

        [Authorize(Roles = "STUDENT")]
        //[HttpPost]
        public ActionResult Work(int labworkId)
        {
            Labwork labwork = LabworkDao.GetById(labworkId);
            User owner = UserDao.GetById(labwork.OwnerId);

            List<PeriphParamModel> periphParamModels = new List<PeriphParamModel>();
            foreach (PeriphParam param in PeriphParamDao.Load(labworkId))
            {
                int countExp = LabworkDao.CountExperiment(labworkId, (int)MmShProvider.GetUser().GroupId);
                for (int index = 0; index < countExp; index++)
                    periphParamModels.Add(
                        new PeriphParamModel { Id = param.Id, Name = param.Name, ExperIndex = index, Value = 0 });
            }
            return View(
                new WorkModel
                    {
                        Id = labwork.Id,
                        Title = labwork.Title,
                        Descr = labwork.Descr,
                        CountExperiments = LabworkDao.CountExperiment(labworkId, (int)MmShProvider.GetUser().GroupId),
                        Owner = string.Format("{0} {1}", owner.FirstName, owner.LastName),
                        PeriphParamModels = periphParamModels,
                        Conclusion = ""
                    });
        }

        [Authorize(Roles = "STUDENT")]
        [HttpPost]
        public ActionResult Work(int labworkId, WorkModel workModel)
        {
            using (CmDbEntities db = new CmDbEntities())
            {
                foreach (PeriphParamModel periphParamModel in workModel.PeriphParamModels)
                {
                    db.AddToRelLabworkPeriphParamUser(
                        new RelLabworkPeriphParamUser
                            {
                                LabworkId = labworkId,
                                UserId = MmShProvider.GetUser().Id,
                                ParameterId = periphParamModel.Id,
                                Experiment = periphParamModel.ExperIndex,
                                Value = periphParamModel.Value
                            });
                    db.SaveChanges();
                }
                db.AddToRelLabworkUserConclusion(
                    new RelLabworkUserConclusion
                        {
                            LabworkId = labworkId,
                            UserId = MmShProvider.GetUser().Id,
                            Conclusion = workModel.Conclusion
                        });
                db.SaveChanges();
            }
            return RedirectToAction("ControlIntro", new { labworkId });
        }

        [Authorize(Roles = "STUDENT")]
        public ActionResult ControlIntro(int labworkId)
        {
            return View(
                new CtrlIntroModel
                    {
                        LabworkId = labworkId,
                        LabworkTitle = LabworkDao.GetById(labworkId).Title,
                        QuestionCount = CtrlQuestionDao.GetCount(labworkId)
                    });
        }

        [Authorize(Roles = "STUDENT")]
        public ActionResult ControlQuestion(int labworkId)
        {
            CtrlQuestion ctrlQues = CtrlQuestionDao.GetUnanswered(labworkId, MmShProvider.GetUser().Id);
            if (ctrlQues == null) return RedirectToAction("WorkResult", new { labworkId });
            return View(
                new CtrlQuestionModel
                    {
                        Id = ctrlQues.Id,
                        Content = ctrlQues.Content,
                        Answer = string.Empty
                    });
        }

        [Authorize(Roles = "STUDENT")]
        [HttpPost]
        public ActionResult ControlQuestion(int labworkId, CtrlQuestionModel ctrlQuestionModel)
        {
            using (CmDbEntities db = new CmDbEntities())
            {
                db.AddToRelLabworkCtrlQuestionUser(
                    new RelLabworkCtrlQuestionUser
                        {
                            LabworkId = labworkId,
                            UserId = MmShProvider.GetUser().Id,
                            ControlQuestionId = ctrlQuestionModel.Id,
                            Answer = ctrlQuestionModel.Answer
                        });
                db.SaveChanges();
            }
            return RedirectToAction("ControlQuestion", new { labworkId });
        }

        [Authorize(Roles = "STUDENT")]
        public ActionResult WorkResult(int labworkId)
        {
            return View(
                new CtrlResultModel
                    {
                        LabworkTitle = LabworkDao.GetById(labworkId).Title,
                        AdmQuestTotalCount = AdmQuestionDao.GetCount(labworkId),
                        AdmQuestRightCount = AdmQuestionDao.GetRightCount(labworkId, MmShProvider.GetUser().Id),
                        CtrlQuestTotalCount = CtrlQuestionDao.GetCount(labworkId),
                        CtrlQuestAnsweredCount = CtrlQuestionDao.GetAnsweredCount(labworkId, MmShProvider.GetUser().Id)
                    });
        }

    }
}
